package com.vlong.town.controller.admin.place;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.place.Place;
import com.vlong.town.service.place.PlaceService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 公共场所
 */
@Controller
@RequestMapping("/place")
public class PlaceController extends BaseController  {

	// Fields

	@Autowired
	private PlaceService placeService;// 业务接口

	// Methods


	/**
	 * 进入
	 * @param pageNo 当前页码
	 * @param pageSize 每页记录数
	 * @return place/add_place
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String gotoPlaceListPage(@RequestParam(required=false, value="pageNo") Integer pageNo,
									@RequestParam(required=false, value="pageSize") Integer pageSize,
									Place place) {
		PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
		//List<Place> placeList = this.placeService.query(place);
		List<Place> placeList = placeService.queryList(null,null,null,null,null,null,null,null,null,null,null,null);
		PageInfo<Place> pageInfo = new PageInfo<>(placeList);
		PageTag<Place> pt = new PageTag<>(request, "list", pageInfo);
		request.setAttribute("pageHtml", pt.getPageHtml());
		for(Place placeInfo : placeList) {
			placeInfo.setPlaceDesc(DataUtil.delHTMLTag(placeInfo.getPlaceDesc(), 0));
		}
		request.setAttribute("placeList",placeList);
		request.setAttribute("title",place.getTitle());
		return "admin/place/list_place";
	}

	/**
	 * Ajax加载
	 * @param pageOffset 当前记录索引
	 * @param pageSize 每页记录数
	 * @return DataMessage->Json
	 */
	@ResponseBody
	@RequestMapping(value="/dataList", method=RequestMethod.POST)
	public DataMessage dataList(@RequestParam(required=false, value="pageOffset") Integer pageOffset, @RequestParam(required=false, value="pageSize") Integer pageSize) {
		DataMessage dm = new DataMessage();
		PageHelper.offsetPage(getPageOffset(pageOffset), getPageSize(pageSize));
		List<Place> placeList = null;
		// List<Place> placeList = this.placeService.queryList(placeId, townId, placeType, title, placeDesc, isDel, status, address, lng, lat, createTime, updateTime);
		PageInfo<Place> pageInfo = new PageInfo<>(placeList);
		dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
		dm.setData(pageInfo.getList());
		dm.setMessage("数据获取成功");
		return dm;
	}

	/**
	 * 进入
	 * @return place/add_place
	 */
	@RequestMapping(value="/gotoAddPage", method=RequestMethod.GET)
	public String gotoPlaceAddPage() {
		return "admin/place/add_place";
	}

	/**
	 * 添加
	 * 这里的参数需要根据实际情况自行修改
	 * @return DataMessage->Json
	 */
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public DataMessage addPlace(Long placeId, Long townId, Integer placeType, String title,String intro, String placeDesc, Integer isDel, Integer status, String address, Float lng, Float lat, Long createTime, Long updateTime) {
		DataMessage dm = new DataMessage();
		// 数据验证自己写吧，只能帮你到这里了～
		this.placeService.add(placeId, townId, placeType, title,intro, placeDesc, isDel, status, address, lng, lat, createTime, updateTime);
		dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
		// dm.setData("这里放需要返回到数据");
		dm.setMessage("数据添加成功");
		return dm;
	}

	/**
	 * 进入
	 * @param placeId 主键值
	 * @return place/edit_place
	 */
	@GetMapping("/gotoEditPage/{placeId}")
	public String gotoPlaceEditPage(@PathVariable( value="placeId") Long placeId) {
		Place editplace = this.placeService.queryById(placeId);
		request.setAttribute("placeInfo", editplace);
		return "admin/place/edit_place";
	}

	/**
	 * 编辑
	 * 这里的参数需要根据实际情况自行修改
	 * @return DataMessage->Json
	 */
	@ResponseBody
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	public DataMessage editPlace(Long placeId, Long townId, Integer placeType, String title,String intro, String placeDesc, Integer isDel, Integer status, String address, Float lng, Float lat, Long createTime, Long updateTime) {
		DataMessage dm = new DataMessage();
		// 数据验证自己写吧，只能帮你到这里了～
		this.placeService.edit(placeId, townId, placeType, title,intro, placeDesc, isDel, status, address, lng, lat, createTime, updateTime);
		dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
		// dm.setData("这里放需要返回到数据");
		dm.setMessage("数据添加成功");
		return dm;
	}

	/**
	 * 删除
	 * @param placeId 主键值
	 * @return DataMessage->Json
	 */
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public DataMessage deletePlace(@RequestParam( value="placeId") Long placeId) {
		DataMessage dm = new DataMessage();
		if(placeId != null && placeId > 0L) {
			this.placeService.edit(placeId, null, null, null, null,null, 1, null, null, null, null, null, System.currentTimeMillis());
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setData(placeId);
			dm.setMessage("数据删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}

	/**
	 * 批量删除公共场所
	 * @param placeIds 主键值集合字符串
	 * @return DataMessage->Json
	 */
	@ResponseBody
	@PostMapping("/batchDelete")
	public DataMessage batchDeleteUser(@RequestParam(value="placeIds") String placeIds) {
		DataMessage dm = new DataMessage();
		if(this.validateStringProperty(placeIds, 1, 255)) {
			String[] placeIdArray = placeIds.split(",");
			for(String placeId : placeIdArray) {
				this.placeService.edit(Long.parseLong(placeId),null,null,null,null,null,null,null,null,null,null,null,null);
			}
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("公共场所删除成功");
		} else {
			dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
			dm.setMessage("参数错误");
		}
		return dm;
	}

}